/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

var socket = io.connect();

$(document).ready(function() {
    socket.on('userLoggedIn', function(data) {

    });

    socket.on('chatMessage', function(data) {
        addMessage(data['message'], data['username']);
    });
});

function addMessage(msg, pseudo) {
    $('#chatEntries').append('<div class="message"><p>' + pseudo + ' : ' + msg + '</p></div>');
}

function sentMessage() {
    if ($('#sendTo').val() != '' && $('#messageInput').val() != '') {
        socket.emit('chatMessage', {
            sendTo: $('#sendTo').val(),
            message: $('#messageInput').val()
        });
        addMessage($('#messageInput').val(), 'Me', new Date().toISOString(), true);
        $('#messageInput').val('');
    }
}

function setUserInfo() {
    if ($('#id').val() != '' && $("#username").val() != '') {
        socket.emit('register', {
            id: $("#id").val(),
            username: $("#username").val()
        });
        $('#chatControls').show();
        $('#userInfo').hide();
    }
}

//  Initializations
$(function() {
    $('#chatControls').hide();
    $('#setUserInfo').click(function() {
        setUserInfo();
    });
    $('#sendMessage').click(function() {
        sentMessage();
    });
});